package com.longdaji.decoration.ui.login.passwordLogin;

import android.text.TextUtils;

import com.longdaji.decoration.Constants;
import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.bean.Order;
import com.longdaji.decoration.bean.Orders;
import com.longdaji.decoration.bean.ReceivingAddress;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.data.network.HttpResponse;
import com.longdaji.decoration.utils.LogUtil;
import com.longdaji.decoration.utils.RxUtil;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nimlib.sdk.AbortableFuture;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.LoginInfo;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Kanade on 2018/3/12 0012.
 */

public class PasswordLoginPresenter extends RxPresenter<PasswordLoginContract.IView> implements PasswordLoginContract.IPresenter {
    private static final String TAG = "PasswordLoginPresenter";
    private DataManager mDataManager;

    private static final String INTERFACE_TEST = "interface test";
    private static final String TEST_USER_ID = "352a01f9a2244c55b4dbf0255bf892db";
    private AbortableFuture<LoginInfo> loginRequest;
//    a2779075e6ce40788bb54a9c098abebb

    @Inject
    public PasswordLoginPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }

    @Override
    public void onConfirmClick(String account, String password, String loginTime) {
        addSubscribe(mDataManager.doPasswordLoginApi(account, password, loginTime)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<Account>handleResult())
                .subscribeWith(new CommonSubscriber<Account>() {
                    @Override
                    public void onSuccess(final Account account) {
                        LogUtil.d(TAG, "login succeed : msg : " + account.toString());
                        if (account == null) {
                            return;
                        }
                        if (loginRequest != null) {
                            loginRequest.abort();
                            loginRequest = null;
                        }
                        loginRequest = NimUIKit.login(new LoginInfo(account.getUserid(), account.getToken()), new RequestCallback<LoginInfo>() {
                            @Override
                            public void onSuccess(LoginInfo param) {
                                LogUtil.i(TAG, "login success");
                                loginRequest = null;
                                if (mView != null) {
                                    mView.goMainByUsername(account);
                                }

                            }

                            @Override
                            public void onFailed(int code) {
                                loginRequest = null;
                                if (mView != null) {
                                    mView.showNimLoginFail();
                                }
                            }

                            @Override
                            public void onException(Throwable exception) {
                                loginRequest = null;
                                if (mView != null) {
                                    mView.showNimLoginFail();
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d(TAG, "login failed : msg : " + msg);
                        // TODO: 2018/4/27 0027 这里要将封装解开一点，走错误回调，看不到具体的错误信息
                        //D/RxUtil: response is HttpResponse{message='用户名或密码错误', statusCode=-1000, resultData=null}
                        //D/PasswordLoginPresenter: login failed : msg : com.longdaji.decoration.data.network.ApiException: 服务器返回错误
                        // TODO: 2018/5/27 0027 细化登录失败的几种情况
                        if (mView != null) {
                            mView.showAccountError();
                        }

                    }
                })
        );

    }

    @Override
    public void onHidePasswordClick(boolean isPasswordHidden) {
        if (isPasswordHidden) {
            mView.showHidePassword(false);
        } else {
            mView.showHidePassword(true);
        }
    }

    // TODO: 2018/3/28 0028 只需要在注册时候传入0和1，后面都可以设置为男和女
    private void initAccount() {
        if (TextUtils.equals("0", Account.getInstance().getGender())) {
            Account.getInstance().setGender("男");
        } else if (TextUtils.equals("1", Account.getInstance().getGender())) {
            Account.getInstance().setGender("女");
        }
    }

    //interface test
    @Override
    public void doInterfaceTest(int number) {
        switch (number) {
            case 100005:
                //pass
                doUpdatePersonalInfoTest();
                break;
            case 100006:
                doUpdateAccountTest();
                break;
            case 100007:
                doUpdateCampusTest();
                break;
            case 100008:
                doGetUserInfoTest();
                break;
            case 100011:
                doAddReceivingAddressTest();
                break;
            case 100012:
                doGetReceivingAddressTest();
                break;
            case 100013:
                doDeleteReceivingAddressTest();
                break;
            case 100014:
                doUpdateReceivingAddressTest();
                break;
            case 100015:
                doGetDefaultReceivingAddressTest();
                break;
            case 100016:
                //fail
                doGetOrderTest();
                break;
            case 100017:
                doGenerateOrderTest();
                break;
            case 100026:
                doGetUpTokenTest();
                break;
            default:
                mView.clearTestResult();
                break;
        }
    }

    private void doGetUpTokenTest() {
        addSubscribe(mDataManager.doGetUpTokenApi()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        LogUtil.d(INTERFACE_TEST, "succeed : doGetUpTokenTest : " + httpResponse.getData().toString());
                        mView.showTestResult(true);

                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d(INTERFACE_TEST, "failed : " + msg);
                        mView.showTestResult(false);
                    }
                }));
    }

    //pass
    private void doGenerateOrderTest() {
        List<Order.GoodsBean> products = new ArrayList<>();
//        Order.GoodsBean goodsBean = new Order.GoodsBean("0", "0", "颜色：红色,尺寸:xl", 0,
//                "ak", 0, "www.htsc.com.cn", 0, "0", "100021", 3, 0);
        Order.GoodsBean goodsBean = new Order.GoodsBean();
        products.add(goodsBean);
//        products.add(goodsBean);
//        products.add(goodsBean);
        Order order = new Order(0, 0, "0", 0, "2018-09-11 11:00:00",
                "0", "2018-09-10", Constants.ORDER_PENDING_PAYMENT,
                100, 3, 133, "2018-09-09 12:00:00", TEST_USER_ID, products);
        LogUtil.d("ordertest", order.toString());
        addSubscribe(mDataManager.doGenerateOrderApi(order)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        LogUtil.d(INTERFACE_TEST, "succeed : doGenerateOrderTest ");
                        mView.showTestResult(true);
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d(INTERFACE_TEST, "failed : " + msg);
                        mView.showTestResult(false);
                    }
                }));
    }

    //fail
    private void doGetOrderTest() {
        addSubscribe(mDataManager.doGetOrderApi(TEST_USER_ID, null, null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<Orders>handleResult())
                .subscribeWith(new CommonSubscriber<Orders>() {
                    @Override
                    public void onSuccess(Orders orders) {
                        LogUtil.d(INTERFACE_TEST, "succeed : doGetOrderTest ");
                        mView.showTestResult(true);
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d(INTERFACE_TEST, "failed : " + msg);
                        mView.showTestResult(false);
                    }
                }));
    }

    private void doGetDefaultReceivingAddressTest() {
        addSubscribe(mDataManager.doGetDefaultReceivingAddressApi(TEST_USER_ID, null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<ReceivingAddress>handleResult())
                .subscribeWith(new CommonSubscriber<ReceivingAddress>() {
                    @Override
                    public void onSuccess(ReceivingAddress receivingAddress) {
                        LogUtil.d(INTERFACE_TEST, "succeed : doGetDefaultReceivingAddressTest " + receivingAddress.toString());
                        mView.showTestResult(true);
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d(INTERFACE_TEST, "failed : " + msg);
                        mView.showTestResult(false);
                    }
                }));
    }

    //pass
    private void doUpdateReceivingAddressTest() {
        addSubscribe(mDataManager.doUpdateReceivingAddressApi(TEST_USER_ID, null, null,
                null, null, null, null, null, null, null, 6, null, null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        LogUtil.d(INTERFACE_TEST, "succeed : doUpdateReceivingAddressTest ");
                        mView.showTestResult(true);
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d(INTERFACE_TEST, "failed : " + msg);
                        mView.showTestResult(false);
                    }
                }));
    }

    //pass
    private void doDeleteReceivingAddressTest() {
        addSubscribe(mDataManager.doDeleteReceivingAddressApi(TEST_USER_ID, 7)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        LogUtil.d(INTERFACE_TEST, "succeed : doDeleteReceivingAddressTest ");
                        mView.showTestResult(true);
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d(INTERFACE_TEST, "failed : " + msg);
                        mView.showTestResult(false);
                    }
                }));
    }

    //pass
    private void doGetReceivingAddressTest() {
        addSubscribe(mDataManager.doGetReceivingAddressApi(TEST_USER_ID, null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<ReceivingAddress>handleResult())
                .subscribeWith(new CommonSubscriber<ReceivingAddress>() {
                    @Override
                    public void onSuccess(ReceivingAddress receivingAddress) {
                        LogUtil.d(INTERFACE_TEST, "succeed : doGetReceivingAddressTest " + receivingAddress.toString());
                        mView.showTestResult(true);
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d(INTERFACE_TEST, "failed : " + msg);
                        mView.showTestResult(false);
                    }
                }));
    }

    //pass
    private void doAddReceivingAddressTest() {
        addSubscribe(mDataManager.doAddReceivingAddressApi(TEST_USER_ID, "testName", null, null,
                null, null, null, null, null, null, null, null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        LogUtil.d(INTERFACE_TEST, "succeed : doAddReceivingAddressTest ");
                        mView.showTestResult(true);
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d(INTERFACE_TEST, "failed : " + msg);
                        mView.showTestResult(false);
                    }
                }));
    }

    //pass
    private void doGetUserInfoTest() {
        addSubscribe(mDataManager.doGetUserInfoApi(TEST_USER_ID, null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<Account>handleResult())
                .subscribeWith(new CommonSubscriber<Account>() {
                    @Override
                    public void onSuccess(Account account) {
                        LogUtil.d(INTERFACE_TEST, "succeed : doGetUserInfoTest " + account.toString());
                        mView.showTestResult(true);
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d(INTERFACE_TEST, "failed : " + msg);
                        mView.showTestResult(false);
                    }
                }));
    }

    //pass
    private void doUpdateCampusTest() {
        addSubscribe(mDataManager.doUpdateCampusApi(TEST_USER_ID, "卡塞尔学院", "11栋2单元101室")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        LogUtil.d(INTERFACE_TEST, "succeed : doUpdateCampusTest ");
                        mView.showTestResult(true);
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d(INTERFACE_TEST, "failed : " + msg);
                        mView.showTestResult(false);
                    }
                }));
    }

    private void doUpdateAccountTest() {
        //pass
        addSubscribe(mDataManager.doUpdateAccountApi(TEST_USER_ID, null, "13041025597", null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        LogUtil.d(INTERFACE_TEST, "succeed : doUpdateAccountApi ");
                        mView.showTestResult(true);

                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d(INTERFACE_TEST, "failed : " + msg);
                        mView.showTestResult(false);
                    }
                }));

    }

    private void doUpdatePersonalInfoTest() {
        addSubscribe(mDataManager.doUpdatePersonalInfoApi(TEST_USER_ID, null, null, "hahaha", "kawaii",
                null, null, null, 0)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        LogUtil.d(INTERFACE_TEST, "succeed : doUpdatePersonalInfoTest ");
                        mView.showTestResult(true);
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d(INTERFACE_TEST, "failed : " + msg);
                        mView.showTestResult(false);
                    }
                }));
    }
}
